﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Twitterizer;

namespace haber
{
    public partial class twitterCallBack : System.Web.UI.Page
    {
        public string screenName;
        public string userID;
        public string Token;
        public string TokenSecret; 
        int okid = 0;
        string id = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string consumerKey = Request["oauth_token"] as string;
                string consumerSecret = Request["oauth_verifier"] as string;
                id = Request["id"] as string;
                if (string.IsNullOrEmpty(consumerKey) || string.IsNullOrEmpty(consumerSecret))
                {
                    Response.Redirect("~/", true);
                }

                OAuthTokenResponse accessTokenResponse = OAuthUtility.GetAccessTokenDuringCallback(consumerKey, consumerSecret);

                Token = accessTokenResponse.Token;
                TokenSecret = accessTokenResponse.TokenSecret;
                screenName = accessTokenResponse.ScreenName;
                userID = accessTokenResponse.UserId.ToString();
             
                if (id != "")
                {
                    using (dcHaberDataContext dc = new dcHaberDataContext())
                    {
                        var kategori = from k in dc.HaberKategorileris
                                       where k.KategoriID == int.Parse(id)
                                       select k;
                        if (kategori.Count() > 0)
                        {
                            HaberKategorileri hk = kategori.Single();
                            hk.TwitterAccessToken = Token;
                            hk.TwitterAccessTokenSecret = TokenSecret;
                            hk.TwitterConsumerKey = consumerKey;
                            hk.TwitterConsumerSecret = consumerSecret;
                            hk.TwitterScreenName = screenName;
                            hk.twitUID = userID;
                            dc.SubmitChanges();
                            okid = hk.KategoriID;
                        }
                    }
                    //
                }

            }
            catch (Exception ex)
            {
                throw new Exception("Çok Fena Twitter Error :" + ex.Message);
            }
            if (okid>0){
                Response.Redirect("/Yonetim/Default.aspx?katid=" + id);
            }
        }

    }
}